Imports System.Windows.Forms
Imports RIBS.Code.GlobalModule1


Public Class frmWork
    Dim dsForm As DataSet
    Dim ht As New Hashtable

    Dim Stats(20) As String
    Dim valStats(20) As Int32

    Enum AUStates
        VIC = 1
        WA = 2
        ACT = 3
        NSW = 4
        TAS = 5
        NT = 6
        SA = 7
        QLD = 8
        Other = 9
        Total = 10
    End Enum
    Enum ColumnName
        UserID
    End Enum

    Enum ColumnName_Display
        IsRowVisible
        ContactMethod
        Action
        LastUser
        UserID
        RibsState
        Letter_Code
        Description
        Amount_Due
        Policy_Number
        Product_Type
        Mailing_Name
        Mailing_State
        Adviser_Surname
        Adviser_Agent_Number
        Adviser_Client_Number
        Letter_Date
        Due_Date
    End Enum

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        SaveColumnDisplayIndex()
        SaveColumnWidths()
        My.Settings.Save()
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Sub RefreshScr()
        Try
            Cursor = Cursors.WaitCursor
            Dim ActionList As String = ""
            Dim StatusList As String = ""
            SaveColumnDisplayIndex()
            SaveColumnWidths()
            My.Settings.Save()
            'If chkListAction.CheckedIndices.Count = 0 Then ActionList = "None,"

            For Each itm As Object In chkListAction.CheckedIndices
                ActionList += chkListAction.Items(itm) & ","
            Next

            For Each itm As Object In chkListStatus.CheckedIndices
                StatusList += chkListStatus.Items(itm) & ","
            Next

            Dim UserList As String = ""
            'If chkListUsers.CheckedIndices.Count = 0 Then UserList = "None,"
            For Each itm As Object In chkListUsers.CheckedIndices
                UserList += chkListUsers.Items(itm) & ","
            Next

            Dim oWork As New WorkLoad
            dsForm = oWork.GetAvailableWork(ActionList, StatusList, UserList, chkStatistics.Checked, txtFind.Text.Trim)
            Filter()
            If IsNothing(dsForm) Then
                dgWork.DataSource = dsForm
                lstStatistics.Items.Clear()
            Else
                'Dim drFilter() As DataRow = ds.Tables("Work").Select("UserID ='Unallocated'")
                'dgWork.DataSource = Nothing
                'dgWork.AutoGenerateColumns = False

                'For Each dgCol As DataColumn In ds.Tables("Work").Columns
                '    dgWork.Columns.Add(dgCol.ColumnName, dgCol.ColumnName)
                'Next
                'dgWork.Rows.Clear()
                'For Each drF As DataRow In drFilter
                '    dgWork.Rows.Add(drF.ToString)
                'Next
                'dgWork.Rows.Add()
                DisplayGrid()
            End If
            Me.Show()
            Me.Focus()
        Catch ex As Exception
            Cursor = Cursors.Default
            DisplayErrors(ex)
        Finally
            Cursor = Cursors.Default
        End Try
    End Sub
    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        RefreshScr()
    End Sub

    Private Sub AddComboBoxColumns()
        Dim comboboxColumn As DataGridViewComboBoxColumn
        comboboxColumn = CreateComboBoxColumn()
        SetAlternateChoicesUsingDataSource(comboboxColumn)
        comboboxColumn.HeaderText = "UserID"
        dgWork.Columns.Insert(0, comboboxColumn)
        dgWork.Columns(0).ReadOnly = False

    End Sub

    Private Function CreateComboBoxColumn() _
        As DataGridViewComboBoxColumn
        Dim column As New DataGridViewComboBoxColumn()

        With column
            .DataPropertyName = ColumnName.UserID.ToString()
            .HeaderText = ColumnName.UserID.ToString()
            .DropDownWidth = 160
            .Width = 90
            .MaxDropDownItems = 7
            .FlatStyle = FlatStyle.Flat
        End With
        Return column
    End Function

    Private Sub SetAlternateChoicesUsingDataSource( _
            ByVal comboboxColumn As DataGridViewComboBoxColumn)
        With comboboxColumn
            .DataSource = RetrieveUsers()
            .ValueMember = ColumnName.UserID.ToString()
            .DisplayMember = .ValueMember
        End With
    End Sub

    Public Function RetrieveUsers() As DataTable
        Dim oUsr As New dbUser
        Return oUsr.GetAllUsers
    End Function

    Private Sub frmWork_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            Dim oRibs As New RibsClass
            Dim dt As DataTable ' = oRibs.GetActions
            dt = oRibs.GetContactMethods
            chkContactMethod.Items.Clear()            
            For Each dr As DataRow In dt.Rows
                If dr(0).ToString = RIBSState.Unallocated.ToString Then
                    chkContactMethod.Items.Add(dr(0), True)
                Else
                    chkContactMethod.Items.Add(dr(0), False)
                End If
            Next
            '
            dt = oRibs.GetActions
            chkListAction.Items.Clear()
            'chkListAction.Items.Add("None", True)
            For Each dr As DataRow In dt.Rows
                If dr(0).ToString = RIBSState.Unallocated.ToString Then
                    chkListAction.Items.Add(dr(0), True)
                Else
                    chkListAction.Items.Add(dr(0), False)
                End If

            Next

            dt = oRibs.GetStates
            chkListStatus.Items.Clear()
            For Each dr As DataRow In dt.Rows
                If dr(0).ToString = RIBSState.Unallocated.ToString Then
                    chkListStatus.Items.Add(dr(0), True)
                Else
                    chkListStatus.Items.Add(dr(0), False)
                End If
            Next

            Dim oUsers As New dbUser
            Dim dtusrs As DataTable = oUsers.GetAllUsers
            chkListUsers.Items.Clear()
            'chkListUsers.Items.Add("None", True)
            For Each dr As DataRow In dtusrs.Rows
                If dr(0).ToString = RIBSState.Unallocated.ToString Then
                    chkListUsers.Items.Add(dr(0).ToString.ToUpper, True)
                Else
                    chkListUsers.Items.Add(dr(0).ToString.ToUpper, False)
                End If
            Next
            comUsers.Text = CurrentUser
            comUsers.DataSource = dtusrs
            comUsers.DisplayMember = "UserName"
            comUsers.ValueMember = "UserID"
            If My.Settings.Statistics Then
                chkStatistics.Checked = True
            End If
            RefreshScr()

        Catch ex As Exception
            DisplayErrors(ex)
        End Try
    End Sub

    Private Sub unAllocated_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnunAllocated.Click
        Try
            Cursor = Cursors.WaitCursor
            Dim SelectedWork As String = ""

            For Each dr As DataGridViewRow In dgWork.Rows
                If dr.Selected Then
                    SelectedWork += dr.Cells("RibsID").Value & "," & RIBSState.Unallocated.ToString & "|"
                Else
                    dr.Visible = False
                End If
            Next

            Application.DoEvents()
            Dim oRibs As New RibsClass
            oRibs.AllocateWork(SelectedWork)
            Cursor = Cursors.Default
            'Me.Close()
            RefreshScr()
        Catch ex As Exception
            Cursor = Cursors.Default
            DisplayErrors(ex)
        End Try
    End Sub

    Private Sub btnAllocate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAllocate.Click
        Try
            Cursor = Cursors.WaitCursor
            Dim SelectedWork As String = ""

            'If comUsers.Enabled Then
            '    For Each dr As DataGridViewRow In dgWork.Rows
            '        If dr.Selected Then
            '            dr.Cells("UserID").Value = CurrentUser
            '            Application.DoEvents()
            '        End If
            '    Next
            'End If

            'Dim ds As DataSet = dgWork.DataSource
            'Dim dschange As DataSet = ds.GetChanges()
            btnRefresh.Focus()
            For Each dr As DataGridViewRow In dgWork.Rows
                If dr.Selected Then
                    SelectedWork += dr.Cells("RibsID").Value & "," & comUsers.Text & "|"
                Else
                    dr.Visible = False
                End If
            Next

            Application.DoEvents()
            Dim oRibs As New RibsClass
            oRibs.AllocateWork(SelectedWork)
            Cursor = Cursors.Default
            'Me.Close()
            RefreshScr()
        Catch ex As Exception
            Cursor = Cursors.Default
            DisplayErrors(ex)
        End Try
    End Sub

    Private Sub dgWork_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgWork.SelectionChanged
        If dgWork.SelectedRows.Count >= 1 Then
            btnAllocate.Text = "Allocate"
            comUsers.Enabled = True
            comUsers.Visible = True
            'comUsers.Visible = True
            chkListUsers.Enabled = False
            btnunAllocated.Enabled = True
            btnAllocate.Enabled = True
        Else
            btnAllocate.Text = "Allocate"
            comUsers.Enabled = False
            chkListUsers.Enabled = True
            btnunAllocated.Enabled = False
            btnAllocate.Enabled = False
        End If
    End Sub

    Private Sub chkStatistics_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkStatistics.CheckedChanged
        My.Settings.Statistics = chkStatistics.Checked
        My.Settings.Save()
    End Sub

    Sub SaveColumnDisplayIndex()
        Try
            Dim col As DataGridViewColumn
            Dim sCols As String = ""
            For Each col In dgWork.Columns
                sCols += col.Name & "|" & col.DisplayIndex & ","
            Next
            My.Settings.WorkColOrder = sCols
        Catch ex As Exception

        End Try
    End Sub

    Sub SaveColumnWidths()
        Try
            Dim col As DataGridViewColumn
            Dim sCols As String = ""
            For Each col In dgWork.Columns
                sCols += col.Width & ","
            Next
            My.Settings.WorkCols = sCols
        Catch ex As Exception

        End Try
    End Sub

    Private Sub frmWork_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        SaveColumnDisplayIndex()
        SaveColumnWidths()
        My.Settings.Save()
    End Sub

    Sub DisplayGrid()
        Try
            dgWork.Enabled = False
            dgWork.VirtualMode = False
            dgWork.DataSource = Nothing
            dgWork.DataSource = dsForm
            dgWork.DataMember = "Work"

            'dgWork.Columns("RibsID").Visible = False
            'dgWork.Columns("UserID").Visible = False

            For Each col As DataGridViewColumn In dgWork.Columns
                col.ReadOnly = True
                col.Visible = False
            Next
            'For Each dr As DataGridViewRow In dgWork.Rows
            '    dr.Visible = False
            'Next

            dgWork.Columns(ColumnName_Display.IsRowVisible.ToString).Visible = False

            dgWork.Columns(ColumnName_Display.LastUser.ToString).Visible = True
            dgWork.Columns(ColumnName_Display.LastUser.ToString).HeaderText = "Last User"

            dgWork.Columns(ColumnName_Display.UserID.ToString).Visible = False
            dgWork.Columns(ColumnName_Display.UserID.ToString).HeaderText = "UserID"

            dgWork.Columns(ColumnName_Display.Description.ToString).Visible = True
            dgWork.Columns(ColumnName_Display.Description.ToString).DisplayIndex = 1

            dgWork.Columns(ColumnName_Display.Action.ToString).Visible = False
            dgWork.Columns(ColumnName_Display.Action.ToString).DisplayIndex = 2

            dgWork.Columns(ColumnName_Display.ContactMethod.ToString).Visible = False

            dgWork.Columns(ColumnName_Display.RibsState.ToString).Visible = False
            dgWork.Columns(ColumnName_Display.RibsState.ToString).DisplayIndex = 3
            dgWork.Columns(ColumnName_Display.RibsState.ToString).HeaderText = "Status"

            dgWork.Columns(ColumnName_Display.Adviser_Surname.ToString).Visible = True
            dgWork.Columns(ColumnName_Display.Adviser_Agent_Number.ToString).Visible = True
            dgWork.Columns(ColumnName_Display.Adviser_Client_Number.ToString).Visible = True

            dgWork.Columns(ColumnName_Display.Adviser_Surname.ToString).Visible = True
            dgWork.Columns(ColumnName_Display.Amount_Due.ToString).Visible = True
            dgWork.Columns(ColumnName_Display.Amount_Due.ToString).DefaultCellStyle.Format = "c"

            dgWork.Columns(ColumnName_Display.Due_Date.ToString).Visible = True
            dgWork.Columns(ColumnName_Display.Letter_Code.ToString).Visible = False
            dgWork.Columns(ColumnName_Display.Letter_Date.ToString).Visible = True
            dgWork.Columns(ColumnName_Display.Letter_Date.ToString).DefaultCellStyle.Format = "dd MMM yyyy"

            dgWork.Columns(ColumnName_Display.Mailing_Name.ToString).Visible = True
            dgWork.Columns(ColumnName_Display.Mailing_Name.ToString).HeaderText = "Client Name"
            dgWork.Columns(ColumnName_Display.Mailing_State.ToString).Visible = True
            dgWork.Columns(ColumnName_Display.Mailing_State.ToString).HeaderText = "State"

            dgWork.Columns(ColumnName_Display.Policy_Number.ToString).Visible = True
            dgWork.Columns(ColumnName_Display.Product_Type.ToString).Visible = True

            lstStatistics.Items.Clear()
            lstStatistics.Items.Add(AUStates.Total.ToString & ": " & vbTab & " (" & ht(AUStates.Total.ToString) & ") ")
            For Each de As DictionaryEntry In ht
                If AUStates.Total.ToString <> de.Key Then
                    lstStatistics.Items.Add(de.Key & ": " & vbTab & " (" & de.Value & ") ")
                End If
            Next

            'lstStatistics.Items.Add("Total Work (" & dsForm.Tables("Work").Rows.Count & ")")
            'If chkStatistics.Checked Then
            '    For Each dt As DataTable In dsForm.Tables
            '        If dt.TableName <> "Work" Then
            '            For Each dr As DataRow In dt.Rows
            '                lstStatistics.Items.Add(" " & dt.TableName & ": " & " (" & dr(1) & ") " & dr(0) & " " & dr(2))
            '            Next
            '        End If
            '    Next
            'End If
            Try
                If My.Settings.WorkCols <> "" Then
                    dgWork.Enabled = False
                    Dim col As DataGridViewColumn
                    Dim sCols() As String = Split(My.Settings.WorkCols, ",")
                    For Each col In dgWork.Columns
                        If col.Index < sCols.GetUpperBound(0) Then
                            col.Width = CInt(sCols(col.Index))
                        End If
                    Next
                End If
                If My.Settings.WorkColOrder <> "" Then
                    dgWork.Enabled = False
                    Dim colName As String
                    Dim col As DataGridViewColumn
                    Dim sCols() As String = Split(My.Settings.WorkColOrder, ",")
                    For Each colName In sCols
                        If Trim(colName) <> "" Then
                            col = dgWork.Columns(Split(colName, "|")(0))
                            col.DisplayIndex = Split(colName, "|")(1)
                        End If
                    Next
                End If
            Catch ex As Exception
            End Try

            'For Each dr As DataGridViewRow In dgWork.Rows                
            '    If CInt(dr.Cells(ColumnName_Display.IsRowVisible.ToString).Value) > 3 Then
            '        dr.Visible = True
            '        dr.Cells(ColumnName_Display.Letter_Code.ToString).Value = "Visible"
            '    Else
            '    End If
            'Next
            dgWork.Enabled = True
            Application.DoEvents()
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Sub Filter()
        Try            
            Application.DoEvents()
            chkListAction.Tag = ""
            Dim iCtr As Int32 = chkListAction.Items.Count
            For i As Int32 = 0 To iCtr - 1
                If chkListAction.GetItemChecked(i) Then
                    chkListAction.Tag += chkListAction.Items(i).ToString & ","
                End If
            Next
            chkListStatus.Tag = ""
            iCtr = chkListStatus.Items.Count
            For i As Int32 = 0 To iCtr - 1
                If chkListStatus.GetItemChecked(i) Then
                    chkListStatus.Tag += chkListStatus.Items(i).ToString & ","
                End If
            Next
            chkContactMethod.Tag = ""
            iCtr = chkContactMethod.Items.Count
            For i As Int32 = 0 To iCtr - 1
                If chkContactMethod.GetItemChecked(i) Then
                    chkContactMethod.Tag += chkContactMethod.Items(i).ToString & ","
                End If
            Next
            chkListUsers.Tag = ""
            iCtr = chkListUsers.Items.Count
            For i As Int32 = 0 To iCtr - 1
                If chkListUsers.GetItemChecked(i) Then
                    chkListUsers.Tag += chkListUsers.Items(i).ToString & ","
                End If
            Next


            For Each dr As DataRow In dsForm.Tables("Work").Rows
                dr(ColumnName_Display.IsRowVisible.ToString) = 0
            Next
            ht.Clear()
            ht.Add(AUStates.ACT.ToString, 0)
            ht.Add(AUStates.NSW.ToString, 0)
            ht.Add(AUStates.NT.ToString, 0)
            ht.Add(AUStates.QLD.ToString, 0)
            ht.Add(AUStates.SA.ToString, 0)
            ht.Add(AUStates.TAS.ToString, 0)
            ht.Add(AUStates.VIC.ToString, 0)
            ht.Add(AUStates.WA.ToString, 0)
            ht.Add(AUStates.Other.ToString, 0)
            ht.Add(AUStates.Total.ToString, 0)

            For Each dr As DataRow In dsForm.Tables("Work").Rows

                If InStr(chkListAction.Tag.ToString.ToUpper, dr(ColumnName_Display.Action.ToString).ToString.ToUpper & ",") > 0 Then
                    dr(ColumnName_Display.IsRowVisible.ToString) = dr(ColumnName_Display.IsRowVisible.ToString) + 1
                End If
                If InStr(chkListStatus.Tag.ToString.ToUpper, dr(ColumnName_Display.RibsState.ToString).ToString.ToUpper & ",") > 0 Then
                    dr(ColumnName_Display.IsRowVisible.ToString) = dr(ColumnName_Display.IsRowVisible.ToString) + 1
                End If
                If InStr(chkListUsers.Tag.ToString.ToUpper, dr(ColumnName_Display.UserID.ToString).ToString.ToUpper & ",") > 0 Then
                    dr(ColumnName_Display.IsRowVisible.ToString) = dr(ColumnName_Display.IsRowVisible.ToString) + 1
                End If
                If InStr(chkContactMethod.Tag.ToString.ToUpper, dr(ColumnName_Display.ContactMethod.ToString).ToString.ToUpper & ",") > 0 Then
                    dr(ColumnName_Display.IsRowVisible.ToString) = dr(ColumnName_Display.IsRowVisible.ToString) + 1
                End If
                If dr(ColumnName_Display.IsRowVisible.ToString) < 4 Then
                    dr.Delete()
                Else
                    If IsDBNull(dr(ColumnName_Display.Mailing_State.ToString.ToUpper)) Then
                        dr(ColumnName_Display.Mailing_State.ToString.ToUpper) = ""
                    End If
                    Select Case dr(ColumnName_Display.Mailing_State.ToString.ToUpper)
                        Case AUStates.ACT.ToString
                            ht(AUStates.ACT.ToString) += 1
                        Case AUStates.NSW.ToString
                            ht(AUStates.NSW.ToString) += 1
                        Case AUStates.NT.ToString
                            ht(AUStates.NT.ToString) += 1
                        Case AUStates.QLD.ToString
                            ht(AUStates.QLD.ToString) += 1
                        Case AUStates.SA.ToString
                            ht(AUStates.SA.ToString) += 1
                        Case AUStates.TAS.ToString
                            ht(AUStates.TAS.ToString) += 1
                        Case AUStates.VIC.ToString
                            ht(AUStates.VIC.ToString) += 1
                        Case AUStates.WA.ToString
                            ht(AUStates.WA.ToString) += 1
                        Case Else
                            ht(AUStates.Other.ToString) += 1
                    End Select
                    ht(AUStates.Total.ToString) += 1
                End If

            Next
            dsForm.Tables("Work").AcceptChanges()
            DisplayGrid()

        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Private Sub btnRefreshScreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Cursor = Cursors.WaitCursor
            Application.DoEvents()
            Filter()
            Cursor = Cursors.Default
        Catch ex As Exception
            DisplayErrors(ex)
        End Try
    End Sub

    Sub CheckAll(ByRef Btn As Button, ByRef chListBox As CheckedListBox)
        Dim iCtr As Int32 = chListBox.Items.Count
        For i As Int32 = 0 To iCtr - 1
            If Btn.Text = "Check All" Then
                chListBox.SetItemChecked(i, True)
            Else
                chListBox.SetItemChecked(i, False)
            End If
        Next
        If Btn.Text = "Check All" Then
            Btn.Text = "Uncheck All"
        Else
            Btn.Text = "Check All"
        End If
    End Sub

    Private Sub btnCheckAllActions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckAllActions.Click
        CheckAll(btnCheckAllActions, chkListAction)
    End Sub

    Private Sub btnCheckAllStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckAllStatus.Click
        CheckAll(btnCheckAllStatus, chkListStatus)
    End Sub

    Private Sub btnCheckAllContactMethod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckAllContactMethod.Click
        CheckAll(btnCheckAllContactMethod, chkContactMethod)
    End Sub

    Private Sub btnCheckAllUsers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckAllUsers.Click
        CheckAll(btnCheckAllUsers, chkListUsers)
    End Sub

    Private Sub txtFind_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFind.KeyUp
        If e.KeyCode = Keys.Enter Then
            Cursor = Cursors.WaitCursor
            Application.DoEvents()
            RefreshScr()
            Cursor = Cursors.Default
        End If
    End Sub

    Private Sub OK_Button_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        SaveColumnDisplayIndex()
        SaveColumnWidths()
        My.Settings.Save()
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub
End Class




